// store/user/getters.ts

import { GetterTree, Getter } from 'vuex'
import { MenuState } from './types'
import { RootState } from '../types'

const categories: Getter<MenuState, RootState> = (state: MenuState) => {
  // 需要axios拉取数据
  const categories: { text: string, href: string }[] = []
  for (const article of state.articles) {
    if (
      !article.category ||
      categories.find(category => category.text === article.category)
    ) {
      continue
    }

    const text = article.category
    categories.push({
      text,
      href: '#!'
    })
  }
  return categories.sort().slice(0, 4)
}
const links: Getter<MenuState, RootState> = (state: MenuState, getters) => {
  return state.items.concat(getters.categories)
}

export const getters: GetterTree<MenuState, RootState> = {
  categories,
  links
}
